/*
 * Copyright 2013 Sigurd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.client.config;

import de.digiway.rapidbreeze.client.model.config.ServerConfigurationResourceRepository;
import de.digiway.rapidbreeze.client.model.config.ServerConfigurationResourceRepositoryFactory;
import de.digiway.rapidbreeze.shared.rest.config.CommonServerConfiguration;
import static de.digiway.rapidbreeze.shared.rest.config.CommonServerConfiguration.PROP_DOWNLOAD_PARALLEL_FAST;
import static de.digiway.rapidbreeze.shared.rest.config.CommonServerConfiguration.PROP_DOWNLOAD_PARALLEL_NORMAL;
import static de.digiway.rapidbreeze.shared.rest.config.CommonServerConfiguration.PROP_DOWNLOAD_PARALLEL_PAUSE;
import static de.digiway.rapidbreeze.shared.rest.config.CommonServerConfiguration.PROP_DOWNLOAD_PARALLEL_SLOW;
import de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed;
import static de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed.FAST;
import static de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed.NORMAL;
import static de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed.SLOW;
import static de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed.STOP;
import java.nio.file.Path;
import org.apache.commons.lang3.Validate;

/**
 *
 * @author Sigurd
 */
public class ServerConfiguration implements CommonServerConfiguration {

    private static ServerConfiguration instance;
    private ServerConfigurationResourceRepository configurationRepository;
    private ServerConfigurationListener configurationListener;

    public static ServerConfiguration getInstance() {
        if (instance == null) {
            instance = new ServerConfiguration();
        }
        return instance;
    }

    private ServerConfiguration() {
        this.configurationRepository = ServerConfigurationResourceRepositoryFactory.getInstance();
        this.configurationListener = new ServerConfigurationListener(configurationRepository);
    }

    public void startMonitoring() {
        configurationListener.startMonitoring();
    }

    public void stopMonitoring() {
        configurationListener.stopMonitoring();
    }

    @Override
    public Path getDownloadTargetFolder() {
        if (!configurationRepository.hasConfiguration(PROP_DOWNLOAD_TARGET_FOLDER)) {
            return null;
        }
        return configurationRepository.getConfiguration(PROP_DOWNLOAD_TARGET_FOLDER);
    }

    @Override
    public void setDownloadTargetFolder(Path folder) {
        configurationRepository.setConfiguration(PROP_DOWNLOAD_TARGET_FOLDER, folder);
    }

    @Override
    public void setDownloadSpeed(DownloadSpeed speed) {
        configurationRepository.setConfiguration(PROP_DOWNLOAD_SPEED, speed);
    }

    @Override
    public DownloadSpeed getDownloadSpeed() {
        if (!configurationRepository.hasConfiguration(PROP_DOWNLOAD_SPEED)) {
            return DownloadSpeed.STOP;
        }
        return configurationRepository.getConfiguration(PROP_DOWNLOAD_SPEED);
    }

    @Override
    public Integer getParallelDownloads(DownloadSpeed downloadSpeed) {
        String prop = getParallelDownloadsProperty(downloadSpeed);
        if (!configurationRepository.hasConfiguration(prop)) {
            return 2;
        }
        return configurationRepository.getConfiguration(prop);
    }

    @Override
    public void setParallelDownloads(DownloadSpeed downloadSpeed, int parallel) {
        configurationRepository.setConfiguration(getParallelDownloadsProperty(downloadSpeed), parallel);
    }

    private String getParallelDownloadsProperty(DownloadSpeed speed) {
        Validate.notNull(speed);
        switch (speed) {
            case STOP:
                return PROP_DOWNLOAD_PARALLEL_PAUSE;
            case SLOW:
                return PROP_DOWNLOAD_PARALLEL_SLOW;
            case NORMAL:
                return PROP_DOWNLOAD_PARALLEL_NORMAL;
            case FAST:
                return PROP_DOWNLOAD_PARALLEL_FAST;
            default:
                return null;
        }
    }

    @Override
    public void setKbPerSecond(DownloadSpeed downloadSpeed, int kb) {
        configurationRepository.setConfiguration(getSpeedDownloadsProperty(downloadSpeed), kb);
    }

    @Override
    public int getKbPerSecond(DownloadSpeed downloadSpeed) {
        if (!configurationRepository.hasConfiguration(getSpeedDownloadsProperty(downloadSpeed))) {
            return 0;
        }
        return configurationRepository.getConfiguration(getSpeedDownloadsProperty(downloadSpeed));
    }

    private String getSpeedDownloadsProperty(DownloadSpeed speed) {
        Validate.notNull(speed);
        switch (speed) {
            case STOP:
                return PROP_DOWNLOAD_SPEED_PAUSE;
            case SLOW:
                return PROP_DOWNLOAD_SPEED_SLOW;
            case NORMAL:
                return PROP_DOWNLOAD_SPEED_NORMAL;
            case FAST:
                return PROP_DOWNLOAD_SPEED_FAST;
            default:
                return null;
        }
    }
}
